package org.hgvc.webapp.ets.jsf.beans.member;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.bo.Agreement;
import org.hgvc.webapp.ets.bo.DownPaymentInstallmentSchema;
import org.hgvc.webapp.ets.bo.InstallmentSchema;
import org.hgvc.webapp.ets.bo.Member;
import org.hgvc.webapp.ets.bo.MemberComp;
import org.hgvc.webapp.ets.bo.MemberInd;
import org.hgvc.webapp.ets.bo.MemberIndAcct;
import org.hgvc.webapp.ets.bo.Membership;
import org.hgvc.webapp.ets.bo.Payment;
import org.hgvc.webapp.ets.bo.PaymentDtl;
import org.hgvc.webapp.ets.bo.SelectObj;
import org.hgvc.webapp.ets.bo.StandingInstruction;
import org.hgvc.webapp.ets.constant.AgreementConstant;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.constant.MemberConstant;
import org.hgvc.webapp.ets.constant.SystemConstant;
import org.hgvc.webapp.ets.spring.service.MasterSetupService;
import org.hgvc.webapp.ets.spring.service.MemberService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.SpringUtils;


public class MemberGiftProspectPaymentBean extends AbstractBean {

	public static final String BEAN_NAME="memberGiftProspectPaymentBean";
	public static final String NAV_SELECT_OPTION="memberSelectPaymentProductAsGift";
	public static final String NAV_PRODUCT_PAYMENT="memberConfirmPaymentProductAsGift";
	public static final String NAV_SELECT_INSTALLMENT_SCHEMA="memberSelectInstallmentSchProductAsGift";
	public static final String NAV_PAYMENT_CONFIRM="memberProductAsGiftAck";
	public static final String NAV_SELECT_DOWN_PAYMENT="memberSelectDownPaymentSchProductAsGift";
	public static final String NAV_PAYMENT_FAILED="";
	private Member member;
	private MemberInd memberInd;
	private MemberComp memberComp;
	private Membership membership;
	private List<MemberIndAcct> list;
	private List<SelectObj> paymentOptionList;
	//private List<SelectItem> paymentMethodList;
	private String selectedPaymentOption;
	//private String paymentMethod;
	private double payAmt=0;
	private MemberService service;
	private StandingInstruction instruction;
	private String chequeno;
	private boolean cashFlag = false;
	private boolean checkFlag = false;
	private boolean creditCardFlag = false;
	private String schemaId;
	private List<SelectItem> schemaSelectList;
	private MasterSetupService setupService;
	private List<InstallmentSchema> schemaList;
	private double deposit;
	private double monthPay;
	private int month;
	private InstallmentSchema schema;
	private Agreement agreement;
	private Payment payment;
	private PaymentDtl paymentDtl;
	private String password;
	private DownPaymentInstallmentSchema downPaymentSchema;
	private String downPaymentId;
	private List<SelectItem>downPaymentSelectList;
	private List<DownPaymentInstallmentSchema> downPaymentList;
	
	private double deMonthPay=0;
	private int deMonth = 0;
	
	
	public double getDeMonthPay() {
		return deMonthPay;
	}
	public void setDeMonthPay(double deMonthPay) {
		this.deMonthPay = deMonthPay;
	}
	public int getDeMonth() {
		return deMonth;
	}
	public void setDeMonth(int deMonth) {
		this.deMonth = deMonth;
	}
	public String getDownPaymentId() {
		return downPaymentId;
	}
	public DownPaymentInstallmentSchema getDownPaymentSchema() {
		return downPaymentSchema;
	}
	public void setDownPaymentSchema(DownPaymentInstallmentSchema downPaymentSchema) {
		this.downPaymentSchema = downPaymentSchema;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Payment getPayment() {
		return payment;
	}
	public void setPayment(Payment payment) {
		this.payment = payment;
	}
	public double getDeposit() {
		return deposit;
	}
	public void setDeposit(double deposit) {
		this.deposit = deposit;
	}
	public double getMonthPay() {
		return monthPay;
	}
	public void setMonthPay(double monthPay) {
		this.monthPay = monthPay;
	}
	public int getMonth() {
		return month;
	}
	public void setMonth(int month) {
		this.month = month;
	}
	public void setSetupService(MasterSetupService setupService) {
		this.setupService = setupService;
	}
	public String getChequeno() {
		return chequeno;
	}
	public void setChequeno(String chequeno) {
		this.chequeno = chequeno;
	}
	public boolean isCashFlag() {
		return cashFlag;
	}
	public void setCashFlag(boolean cashFlag) {
		this.cashFlag = cashFlag;
	}
	public boolean isCheckFlag() {
		return checkFlag;
	}
	public void setCheckFlag(boolean checkFlag) {
		this.checkFlag = checkFlag;
	}
	public boolean isCreditCardFlag() {
		return creditCardFlag;
	}
	public void setCreditCardFlag(boolean creditCardFlag) {
		this.creditCardFlag = creditCardFlag;
	}
	public StandingInstruction getInstruction() {
		return instruction;
	}
	public void setInstruction(StandingInstruction instruction) {
		this.instruction = instruction;
	}
	public double getPayAmt() {
		return payAmt;
	}
	public void setPayAmt(double payAmt) {
		this.payAmt = payAmt;
	}
	public void setService(MemberService service) {
		this.service = service;
	}
	public Membership getMembership() {
		return membership;
	}
	public void setMembership(Membership membership) {
		this.membership = membership;
	}
	public Member getMember() {
		return member;
	}
	public void setMember(Member member) {
		this.member = member;
	}
	public MemberInd getMemberInd() {
		return memberInd;
	}
	public void setMemberInd(MemberInd memberInd) {
		this.memberInd = memberInd;
	}
	public MemberComp getMemberComp() {
		return memberComp;
	}
	public void setMemberComp(MemberComp memberComp) {
		this.memberComp = memberComp;
	}
	public List<MemberIndAcct> getList() {
		return list;
	}
	public void setList(List<MemberIndAcct> list) {
		this.list = list;
	}

	public List<SelectObj> getPaymentOptionList() {

		SelectObj packageObj = new SelectObj();
		packageObj.setId(MemberConstant.MEMBER_PAYMENT_FULL_PACKAGE);
		packageObj.setTitle(SpringUtils.getMessage(MemberConstant.MEMBER_PAYMENT_FULL_PACKAGE, new Object[]{}));

		SelectObj installObj = new SelectObj();
		installObj.setId(MemberConstant.mEMBER_PAYMENT_INSTALLMET_SCHEMA);
		installObj.setTitle(SpringUtils.getMessage(MemberConstant.mEMBER_PAYMENT_INSTALLMET_SCHEMA, new Object[]{}));

		paymentOptionList = new ArrayList<SelectObj>();
		paymentOptionList.add(packageObj);
		paymentOptionList.add(installObj);

		return paymentOptionList;
	}

	public String navSelectOptionAction(){
		return NAV_SELECT_OPTION;
	}
	
	public void paymentListener(ActionEvent actionEvent){
		UIParameter param = (UIParameter)actionEvent.getComponent().findComponent("paymentSelectId");
		selectedPaymentOption = (String)param.getValue();
		
	}
	
	public String navCreatePayment(){
		instruction = new StandingInstruction();
		if(selectedPaymentOption.equalsIgnoreCase(MemberConstant.MEMBER_PAYMENT_FULL_PACKAGE)){
			payAmt = membership.getMembershipFeeDbl();
			return NAV_PRODUCT_PAYMENT;
		}else{
			return NAV_SELECT_INSTALLMENT_SCHEMA;
		}
	}
	
	public List<InstallmentSchema> getSchemaList() {
		try{
			schemaList = setupService.getInstallmentSchemaList();
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return schemaList;
	}
	
	public List<SelectItem> getSchemaSelectList(){
		try{
		schemaSelectList = new ArrayList<SelectItem>();
		
		for(InstallmentSchema sch:getSchemaList()){
			SelectItem item = new SelectItem(sch.getInstallmentSchemaId(),sch.getCode()+" "+sch.getName());
			schemaSelectList.add(item);
		}
		}catch(Exception be){
			be.printStackTrace();
			//initErrorBean(be);
		}
		return schemaSelectList;
	}
	
	public String getSchemaId() {
		return schemaId;
	}
	public void setSchemaId(String schemaId) {
		try{
			this.schemaId = schemaId;
			if(schemaId!=null){
				for(InstallmentSchema sch:schemaList){
					if(sch.getInstallmentSchemaId().equalsIgnoreCase(schemaId)){
						double fee = membership.getMembershipFeeDbl();
						deposit = fee*(sch.getDepositPercentDbl()/100);
						monthPay = (fee - deposit)/sch.getMonthInt();
						monthPay = (Math.round(monthPay*100.0))/100.0;
						month = sch.getMonthInt();
						schema = sch;
						break;
					}
				}
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public String confirmPaymentAction(){
		try{
			agreement = new Agreement();
			agreement.setMember(member);
			agreement.setMembership(membership);
			agreement.setRegType(AgreementConstant.AGREEMENT_GIFT_REG);
			if(schema==null&&downPaymentSchema==null){
				agreement.setPaymentType(AgreementConstant.AGREEMENT_PAYMENT_FULL_PACKAGE);
			}else if(schema!=null&&downPaymentSchema==null){
				agreement.setPaymentType(AgreementConstant.AGREEMENT_PAYMENT_INSTALLMENT_PACKAGE);
				agreement.setInstallmentSchema(schema);
			}else if(schema!=null&&downPaymentSchema!=null){
				agreement.setInstallmentSchema(schema);
				agreement.setDownPaymentInstallmentSchema(downPaymentSchema);
				agreement.setPaymentType(AgreementConstant.AGREEMENT_PAYMENT_INSTALLMENT_DEPOSIT_PACKAGE);
			}
			
			payment = new Payment();
			paymentDtl = new PaymentDtl();
			
			if(member.getMemberType().equalsIgnoreCase(CommonConstant.MEMBER_TYPE_IND)){
				payment = service.createProspectIndividualMember(member, memberInd, list, getThisUser(), payment, paymentDtl, 
						instruction, agreement, payAmt);
			}else{
				payment = service.createProspectCompMember(member, memberComp, getThisUser(), list, payment, paymentDtl, 
						instruction, agreement, payAmt);
			}
			password = SystemConstant.DEFAULT_PASSWORD;
			return NAV_PAYMENT_CONFIRM;
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return NAV_PAYMENT_FAILED;
	}
	
	public String navSelectDownPaymentAction(){
		return NAV_SELECT_DOWN_PAYMENT;
	}
	
	public String removeDownPaymentAction(){
		downPaymentSchema = null;
		return NAV_SELECT_INSTALLMENT_SCHEMA;
	}
	
	public String navSelectInstallmentSch(){
		return NAV_SELECT_INSTALLMENT_SCHEMA;
	}
	
	public void setDownPaymentId(String downPaymentId) {
		this.downPaymentId = downPaymentId;
		for(DownPaymentInstallmentSchema sch:downPaymentList){
			if(sch.getDownPaymentInstallmentSchemaId().equalsIgnoreCase(downPaymentId)){
				deMonth = sch.getMonthInt();
				deMonthPay = deposit/deMonth;
				deMonthPay = (Math.round(deMonthPay*100.0))/100.0;
				downPaymentSchema = sch;
				break;
			}
		}
	}
	
	public List<SelectItem> getDownPaymentSelectList(){
		downPaymentSelectList = new ArrayList<SelectItem>();
		try{
			downPaymentList = setupService.getDownPaymentInstallmentSchemaList();
			for(DownPaymentInstallmentSchema downPayment:downPaymentList){
				SelectItem item = new SelectItem(downPayment.getDownPaymentInstallmentSchemaId(),downPayment.getName());
				downPaymentSelectList.add(item);
			}
			
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return downPaymentSelectList;
	}
	
	public void paymentSchemaActionListener(ActionEvent actionEvent){
		if(downPaymentSchema!=null){
			payAmt = deMonthPay;
		}else{
			payAmt = deposit;
		}
	}
	
	public String navSchemaCreatePayment(){
		instruction = new StandingInstruction();
		return NAV_PRODUCT_PAYMENT;
	}
}